import {api} from './libs/api.js';
let _self;
export const index =  {
	data() {
		return {
			optLock:false,
			formData:{"mark":"","order":{}},
			rules:api.rules,
			order:{"Items":[]},
			storeShow:false,
			storeItem:api.storeItem,
			storeInfo:{},
			numMap:{},
			params:{}
		}
	},
	
	mounted() {
		_self = this;
	},
	onReachBottom:function(e){
		/*this.params.page+=1;
		this.initData();*/
	},
	onLoad:function(option) {
		//this.tabSubItem=option.status;
		this.params=option;
	},
	
	methods: {
		async initData(){
			let retInfo=await api.scanCodeInfo(this.params);
			if(retInfo["order"]) this.order = retInfo["order"];		
			if(this.order["Items"]&&this.order["Items"].length>0){
				for(let index in this.order["Items"]){
					let item=this.order["Items"][index];
					this.numMap[item["cInvCode"]]=item["iQuantity"];
				}
			}
		},
		
		/**
		 * 价格数量计算
		 * @param {Object} index
		 */
		computeData(index){
			setTimeout(function(){
				let item=_self.order.Items[index];
				let maxNum=_self.numMap[item.cInvCode];
				let iQuantity=_self.order.Items[index]["iQuantity"];
				if(Number(iQuantity)>Number(maxNum)||isNaN(Number(iQuantity))) iQuantity=maxNum;	
				_self.order.Items[index]["iQuantity"]=iQuantity;
				_self.order.Items[index]["iSum"]=Number(iQuantity)*Number(item["iTaxPrice"]);
			},500);　			
		},

		delProduct(index){
			if(this.order.Items.length==1){
				uni.showToast({icon:"none",title:"至少保留一条记录！"});
				return false;
			}
			this.order.Items.splice(index,1);
		},
		
		async submitForm(){
			if(this.optLock) return true;
			this.optLock=true;
			this.formData["order"]=this.order;
			this.formData["store"]=this.storeInfo;			
			let ret=await api.u8OrderRet(this.formData);
			if(ret=="success"){
				uni.showToast({icon:"none",title:"提交成功！"});
				setTimeout(function(){uni.switchTab({url: "/pages/product/index"});},2000);　　
			}else{
				this.optLock=false;
			}
		}
				
	},
}